--[[
Name: BrownieHelper
Developed by: Sonora (The Dragon Flight)
Website: http://sonora.kirintor.googlepages.com/contents
SVN: http://code.google.com/p/browniehelper/source/browse
License: GNU General Public License v3
]]

function ViewPointsFrame:Initialize(app)
    --app is the brownie helper app
    
    --Register for gui events
    self:SetScript("OnShow", function() self:OnShow() end)
    self:SetScript("OnHide", function() self:OnHide() end)
    
    --Save the brownie helper app
    self.app = app
    
    --Setup the points table
    ViewPointsTable:setFields({'Player','Points'})
    
    --Register a selection handler
    ViewPointsTable:registerSelectionCallback(function(row, selected) self:playerRowSelected(row, selected) end)
    ViewPointsTable:registerDoubleClickCallback(function(row) if ViewPointsTable:getSelections()[1] then self:OnEditPoints() end end)
    self:playerRowSelected(nil,false)
    
    
    --Register a callback to update the view when the points or mode change
    self.app:registerEvent("BrownieHelper_PointsChanged", function() self:updateView() end)
    self.app:registerEvent("BrownieHelper_ModeChanged", function(mode) self:OnModeChange(mode) end)
    --MIGHT not need to call this self:OnModeChange(self.app:getMode())
    
    --initialize the view
    self:updateView()
    
end

function ViewPointsFrame:playerRowSelected(row, selected)
    if self.app:getMode()>=BROWNIEHELPER_MASTERLOOTER and selected==true then
        ViewPointsEditPointsButton:Enable()
    else
        ViewPointsEditPointsButton:Disable()
    end
end


--Event hander
function ViewPointsFrame:OnModeChange(mode)
    if mode>=BROWNIEHELPER_MASTERLOOTER then
        ViewPointsAddPlayerButton:Enable()
        self:OnHide()
    else
        ViewPointsAddPlayerButton:Disable()
    end
    self:playerRowSelected(nil, ViewPointsTable:getSelections()[1])
end

function ViewPointsFrame:OnShow()
    local mode = self.app:getMode()
    if mode<BROWNIEHELPER_MASTERLOOTER and mode>BROWNIEHELPER_QUIESCENT then
        self.app:registerForCommand(self.app:getMasterLooter(),"GetPointUpdates")
    end
end
function ViewPointsFrame:OnHide()
    local mode = self.app:getMode()
    if mode<BROWNIEHELPER_MASTERLOOTER and mode>BROWNIEHELPER_QUIESCENT then
        self.app:unregisterForCommand(self.app:getMasterLooter(),"GetPointUpdates")
    end
end

function ViewPointsFrame:updateView()

    --Get the current pool info
    local pool_name, pool_db = self.app:getCurrentPointPool()
    
    --Update the point pool label
    ViewPointsCurrentPoolLabel:SetText(pool_name)
    
    --Assume we're only open if there is a current run
    ViewPointsTable:clearRows()
    for player_name, points in pairs(pool_db) do
        ViewPointsTable:addRow({["Player"]=player_name, ["Points"]=tonumber(points)})
    end
    
    --Clear the selection
    self:playerRowSelected(nil,false)

end

function ViewPointsFrame:OnClose() self:Hide() end

function ViewPointsFrame:OnAddPlayer()
    
    function handler(response)
        pool_name, point_pool = self.app:getCurrentPointPool()
        point_pool[response] = tostring(0)
        self.app:TriggerEvent("BrownieHelper_PointsChanged")
    end
    
    inputDialog("Enter the name of the new player:", handler, "")
    
end

function ViewPointsFrame:OnEditPoints()
    local playerName = ViewPointsTable:getSelections()[1].Player
    local pool_name, point_pool = self.app:getCurrentPointPool()
    
    function handler(pointResponse)
        
        function noteHandler(noteResponse)
            
            local oldpoints = point_pool[playerName]
            point_pool[playerName] = pointResponse
            self.app:addEvent("Adjusted points", {note=noteResponse,character=self.app:getCharacterName(playerName),player=playerName,oldpoints=tostring(oldpoints),newpoints=pointResponse,description="Points were manually adjusted for "..playerName.."."})
            self.app:TriggerEvent("BrownieHelper_PointsChanged")
            self.app:sendWhisperMessage(self.app:getCharacterName(playerName), "Your points for "..pool_name.." have been adjusted, you now have: "..pointResponse)
            
        end
        
        inputDialog("Enter a description of why the player's points were adjusted:", noteHandler, "")
    end
    
    inputDialog("Enter the new number of points to assign to "..playerName..":", handler,tostring(point_pool[playerName]))
    
end

